package lex.dm.ui.actions;

import java.awt.event.ActionEvent;
import java.sql.Connection;
import javax.swing.SwingWorker;

import org.lex.swing.ActionBuilder;

import lex.dm.DTree;
import lex.dm.ui.ProgressDialog;
import lex.dm.utils.DBUtils;

public class ConnectAction extends ActionBuilder
{
	public static void connect()
	{

		final DTree app = DTree.getApplication();
		final ProgressDialog pd = new ProgressDialog( app.getRootWindow() );
		pd.setLocationRelativeTo( app.getRootWindow() );
		pd.setTitle( "Connect" );
		pd.setPrompt( "" );
		pd.setMessage( "Connecting using Derby Embed Mode" );
		new SwingWorker<Connection, Void>(){
			@Override
			protected Connection doInBackground() throws Exception
			{
				return DBUtils.connect();
			}

			@Override
			protected void done()
			{
				pd.done();
				pd.dispose();
				try
				{
					app.setConnection( this.get() );
				}
				catch( Exception e )
				{
					e.printStackTrace();
				}
			}
		}.execute();
		pd.setVisible( true );
	}
	public ConnectAction()
	{
		this.setName( "Connect" );
		this.setEnabled( true );
	}

	@Override
	public void actionPerformed( ActionEvent e )
	{
		connect();
	}
}
